package com.demo.base.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    public static String getAge(Date birthDate) {
        if (birthDate == null) {
            return "未知";
        }

        int age = 0;

        Date now = new Date();

        SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
        SimpleDateFormat format_M = new SimpleDateFormat("MM");

        String birth_year = format_y.format(birthDate);
        String this_year = format_y.format(now);

        String birth_month = format_M.format(birthDate);
        String this_month = format_M.format(now);

        // 初步，估算
        age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);

        // 如果未到出生月份，则age - 1
        if (this_month.compareTo(birth_month) < 0)
            age -= 1;
        if (age < 0)
            age = 0;
        return age + "";
    }

    public static String formatDate(Date date, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(date);
        } catch (Exception e) {
        }
        return "";
    }

    public static String formatCurrentDate() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sdf.format(new Date());
        } catch (Exception e) {
        }
        return "";
    }

    public static Date parseDate(String dateStr, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(dateStr);
        } catch (Exception e) {
        }
        return null;
    }

    public static long getTime(Date date) {
        if (date != null) {
            return date.getTime();
        }
        return 0;
    }

    public static void addDay(Calendar cal, Integer day) {
        if (day != null) {
            cal.add(Calendar.DATE, day);
        }
    }

    public static void addWeek(Calendar cal, Integer week) {
        if (week != null) {
            cal.add(Calendar.DATE, week * 7);
        }
    }

    public static void addMonth(Calendar cal, Integer month) {
        if (month != null) {
            cal.add(Calendar.MONTH, month);
        }
    }

    public static void addYear(Calendar cal, Integer year) {
        if (year != null) {
            cal.add(Calendar.YEAR, year);
        }
    }

    public static void addHour(Calendar cal, Integer hour) {
        if (hour != null) {
            cal.add(Calendar.HOUR, hour);
        }
    }

    public static void addMinutes(Calendar cal, Integer minute) {
        if (minute != null) {
            cal.add(Calendar.MINUTE, minute);
        }
    }

    public static Date calculateDateByMinutes(Integer minute) {
        Calendar calendar = new GregorianCalendar();
        DateUtils.addMinutes(calendar, minute);
        return calendar.getTime();
    }

    public static String getTimeDesc(String unit) {
        if (unit.equals("hour")) {
            return "小时";
        } else if (unit.equals("minute")) {
            return "分钟";
        }
        return "";
    }

    public static String getChinaDate(Calendar date) {
        int hour = date.get(Calendar.HOUR_OF_DAY);
        int minute = date.get(Calendar.MINUTE);
        return date.get(Calendar.YEAR) + "年" + (date.get(Calendar.MONTH) + 1) + "月" + date.get(Calendar.DATE) + "日  " + (hour >= 10 ? hour : "0" + hour)
                + ":" + (minute >= 10 ? minute : "0" + minute);
    }

    public static String getChinaDate(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        return getChinaDate(instance);
    }

    /**
     * @param date
     * @return 指定时间那一天的开始时间
     */
    public static Date getDayStart(Date date) {
        Calendar calendar = new GregorianCalendar();
        if(date!=null){
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * @param date
     * @return 指定时间那一天的结束时间
     */
    public static Date getDayEnd(Date date) {
        Calendar calendar = new GregorianCalendar();
        if(date!=null){
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }


    /**
     * @return 提前一天
     */
    public static Date getYesterday(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

}

